package com.umeox.mediaplayer;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.yc.music.model.AudioBean;

import java.io.File;


public class ConfirmDeleteActivity extends ThemeAppCompatActivity {

    private TextView mMessageTv;
    private ImageView mConfirmTv;

    private static final int DELETE_COMPLETE = 0x10;

    private AudioBean mAudioBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_confirm_delete);
        mAudioBean = (AudioBean) getIntent().getSerializableExtra("mCurrentAudio");
        mMessageTv = findViewById(R.id.tvMessage);
        mConfirmTv = findViewById(R.id.abConfirm);

        mMessageTv.setText(getString(R.string.confirm_delete,mAudioBean.getTitle()));

        mConfirmTv.setOnClickListener(v -> {
            File file = new File(mAudioBean.getPath());
            if(file.exists()){
                file.delete();
                mHandler.sendEmptyMessage(DELETE_COMPLETE);
            }
        });

    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == DELETE_COMPLETE) {
                Intent intent = new Intent();
                intent.setAction("com.umeox.intent.ACTION_REFRESH");
                LocalBroadcastManager.getInstance(ConfirmDeleteActivity.this).sendBroadcast(intent);
                finish();
            }
            return false;
        }
    });
}
